package com.gexiao.context.annotation;

import com.gexiao.bean.Rainbow;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 导入bean时对bean的定义注册类
 * {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar}
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Boolean blueBeanDefinition = registry.containsBeanDefinition("com.gexiao.bean.Blue");
        Boolean yellowBeanDefinition = registry.containsBeanDefinition("com.gexiao.bean.Yellow");
        if (blueBeanDefinition && yellowBeanDefinition){
            AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(Rainbow.class);
            registry.registerBeanDefinition("rainbow", beanDefinition);
        }
    }
}
